{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "eb0ff442",
   "metadata": {},
   "source": [
    "## Serise的使用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dd216057",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "84007612",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.arange(1,5))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "bed85e23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name      虾米\n",
       "age       20\n",
       "class     三版\n",
       "sex      NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {\n",
    "    'name':'虾米',\n",
    "    'age':20,\n",
    "    'class':'三版'\n",
    "}\n",
    "s2=pd.Series(d,index=['name','age','class','sex'])\n",
    "s2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3de9ad92",
   "metadata": {},
   "source": [
    "### 1.isnull 函数和notnull  函数检查缺失值\n",
    "'''\n",
    "isnull函数：\n",
    "This function takes a scalar or array-like object and indicates\n",
    "whether values are missing (``NaN`` in numeric arrays, ``None`` or ``NaN``\n",
    "in object arrays, ``NaT`` in datetimelike).\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "0c9b3557",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1    False\n",
       "2    False\n",
       "3    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ee31f291",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name     False\n",
       "age      False\n",
       "class    False\n",
       "sex       True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ff5f9479",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, None]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = [0,1,2,None]\n",
    "print(arr)\n",
    "#针对python序列如果使用arr.isnull会报错；但使用pd.isnull()可以检验python列表中是否存在缺失值。\n",
    "pd.isnull(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1b0fd6e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True, False])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断是否不为缺失值。返回值是numpy数组。\n",
    "pd.notnull(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da96bb84",
   "metadata": {},
   "source": [
    "### 2. 通过索引获取数据。使用.values和.index函数单独获取数值和索引值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "7f081cb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n",
      "<class 'numpy.ndarray'>\n",
      "********************\n",
      "RangeIndex(start=0, stop=4, step=1)\n",
      "<class 'pandas.core.indexes.range.RangeIndex'>\n"
     ]
    }
   ],
   "source": [
    "# 由于pandas是基于numpy构建的，因此返回值为numpy数据。\n",
    "print(s.values)\n",
    "print(type(s.values))\n",
    "print('*'*20)\n",
    "print(s.index)\n",
    "print(type(s.index))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "ec1f5fa0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "<class 'numpy.int64'>\n"
     ]
    }
   ],
   "source": [
    "#使用索引获取一个单独一行的数据。此处索引为默认索引0\n",
    "print(s[0])\n",
    "print(type(s[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "f9cb1e01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    0\n",
       "2    1\n",
       "3    2\n",
       "4    3\n",
       "5    4\n",
       "6    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3 = pd.Series(np.arange(6),index=[1,2,3,4,5,6])\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "bc7ed318",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m   3360\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3361\u001b[0;31m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcasted_key\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   3362\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.Int64HashTable.get_item\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.Int64HashTable.get_item\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 0",
      "\nThe above exception was the direct cause of the following exception:\n",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_35826/3777777784.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m#指定标签后，Series序列就会变成标签名，原有的默认索引失效。\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ms3\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/series.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m    940\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    941\u001b[0m         \u001b[0;32melif\u001b[0m \u001b[0mkey_is_scalar\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 942\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    943\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    944\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mis_hashable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/series.py\u001b[0m in \u001b[0;36m_get_value\u001b[0;34m(self, label, takeable)\u001b[0m\n\u001b[1;32m   1049\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1050\u001b[0m         \u001b[0;31m# Similar to Index.get_value, but we do not fall back to positional\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1051\u001b[0;31m         \u001b[0mloc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1052\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_values_for_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mloc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1053\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m   3361\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcasted_key\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   3362\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3363\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   3364\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   3365\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mis_scalar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0misna\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhasnans\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "#指定标签后，Series序列就会变成标签名，原有的默认索引失效。\n",
    "s3[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b5cd335b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0\n",
      "1    1\n",
      "dtype: int64\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "#还可以使用切片的形式获取Serise部分的数据，返回Serise格式的数据\n",
    "print(s[0:2])\n",
    "print(type(s[0:2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "3dd86c59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name     虾米\n",
       "class    三版\n",
       "dtype: object"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#多个标签名时需要双层的[]\n",
    "s2[['name','class']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "53c829de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name    虾米\n",
      "age     20\n",
      "dtype: object\n",
      "********************\n",
      "name     虾米\n",
      "age      20\n",
      "class    三版\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "#下标数字切片(与双层的[]标签切片一致)和标签切片存在区别。\n",
    "print(s2[0:2])\n",
    "print('*'*20)\n",
    "print(s2['name':'class'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "0745c527",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1    False\n",
       "2     True\n",
       "3     True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#布尔索引\n",
    "s>2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "945302e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2    3\n",
       "3    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#布尔索引获取数据，原理同numpy相同\n",
    "s[s>2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "015e9568",
   "metadata": {},
   "source": [
    "### 3.索引与数据的对应关系不被运算结果影响。\n",
    "\n",
    "索引得出的数据是一种视图。这与numpy相同。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "13bec7af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    2\n",
      "1    4\n",
      "2    6\n",
      "3    8\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "#实际上运算得出的结果是一种视图，不改变原数据。\n",
    "print(s*2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "dc1a8f66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fdf8a66",
   "metadata": {},
   "source": [
    "### 4. Serise对象的name属性。及其他常用的行数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "b78cd468",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "year\n",
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    4\n",
       "Name: temp, dtype: int64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.name = 'temp'  # 序列对象名\n",
    "s.index.name='year'  #对象索引名\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "0db6ee5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "year\n",
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "Name: temp, dtype: int64"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取前几行的数据函数。默认前五行。也可指定行数。\n",
    "s.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "1094bfd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "year\n",
       "2    3\n",
       "3    4\n",
       "Name: temp, dtype: int64"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取数据的后几行。默认后五行。也可指定行数。\n",
    "s.tail(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "582bbbb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.isnull?"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
